package com.fg.estate.config;

import com.fg.estate.constant.AppContants;
import com.fg.estate.interceptor.AdminInterceptor;
import com.fg.estate.interceptor.AdminPermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 丰哥
 * 2022/10/13
 *
 * WEBMVC 全局配置类
 */
@Configuration
public class AppConfig implements WebMvcConfigurer {

    @Autowired
    private AdminPermissionInterceptor adminPermissionInterceptor;

    // 物理路径和虚拟路径的映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:" + AppContants.FILE_PATH);
    }

    // 添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加拦截器
        InterceptorRegistration registration = registry.addInterceptor(new AdminInterceptor());
        //设置拦截的路径
        registration.addPathPatterns("/indexUi");


        // 添加管理员权限拦截器
        // admin/**，拦截admin子目录及多层子目录的所有请求， admin/a, admin/a/a
        registry.addInterceptor(adminPermissionInterceptor).addPathPatterns("/admin/**");
    }
}
